/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.gov.mtc.dgac.licencias.util;

import java.util.List;

/**
 *
 * @author externo.gamaro
 */
public class Page {
   private int page;
   private int pageSize;
   private int totalElements;
   private List results;
   private int firstPageElement;
   private int lastPageElement;
   private int numberTotalPages;
   public static final int SIZE_PAGE=5; // 5 registros por pagina en las busquedas

    public int getNumberTotalPages() {
        return numberTotalPages;
    }

    public void setNumberTotalPages(int numberTotalPages) {
        this.numberTotalPages = numberTotalPages;
    }
   
    public int getFirstPageElement() {
        
        return firstPageElement;
    }

    public void setFirstPageElement(int firstPageElement) {
        this.firstPageElement = firstPageElement;
    }

    public int getLastPageElement() {
        return lastPageElement;
    }

    public void setLastPageElement(int lastPageElement) {
        this.lastPageElement = lastPageElement;
    }
   
    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalElements() {
        return totalElements;
    }

    public void setTotalElements(int totalElements) {
        this.totalElements = totalElements;
    }
    public Page() {
    }
    public Page(List results,int page,int pageSize,int totalElements){
      this.page=page;
      this.pageSize=pageSize;
      this.totalElements=totalElements;
      this.results=results;
      this.firstPageElement=getStartItemByPage(page, pageSize);
      
      if((totalElements-firstPageElement)<pageSize) {
        this.lastPageElement=totalElements;
      }else{
        this.lastPageElement=firstPageElement+pageSize-1;
      }
      if(((int) totalElements%pageSize)>0){
        this.numberTotalPages=(totalElements / pageSize)+ 1;  
      }else{
        this.numberTotalPages=totalElements / pageSize;
      }
    }

    public List getResults() {
        return results;
    }

    public void setResults(List results) {
        this.results = results;
    }
    public static int getStartItemByPage(int currentPage, int pageSize) {
        return Math.max((pageSize * (currentPage - 1)) + 1, 1);
        // El Math.max sirve para evitar los numeros negativos, siempre hay
        // algun malvado que puede pedirnos la pagina -10. En esos casos,
        // la pagina devuelta sera 1
    }
}
